﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    class BizDotThi
    {
        public static List<DotThi> GetList(string idHocKy, CEMS4UG_DHKHContext dbContext = null)
        {
            if (dbContext != null)
            {
                return dbContext.DotThiDbSet.Where(e => e.MaHocKyGUID == idHocKy).OrderByDescending(e => e.TenDotThi).ToList();
            }
            else
                using (var db = CEMS4UG_DHKHContext.GetContext())
                {
                    return db.DotThiDbSet.Where(e => e.MaHocKyGUID == idHocKy).OrderByDescending(e => e.TenDotThi).ToList();
                }
        }

        public static CODE_RESULT_RETURN Add(DotThi obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _hp = db.DotThiDbSet.Where(e => e.MaHocKyGUID == obj.MaHocKyGUID && e.TenDotThi == obj.TenDotThi).FirstOrDefault();
                if (_hp != null)
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaDotThiGUID = Guid.NewGuid().ToString();
                db.DotThiDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DotThi>(nhatKy, null, obj.MaDotThiGUID, LOAIDOITUONG.ĐỢT_THI, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<DotThi>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(DotThi obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var dt = db.DotThiDbSet.Where(e => e.TenDotThi == obj.TenDotThi && e.MaHocKyGUID == obj.MaHocKyGUID && e.MaDotThiGUID != obj.MaDotThiGUID).FirstOrDefault();
                if (dt != null)
                    return CODE_RESULT_RETURN.TenTrung;
                dt = db.DotThiDbSet.Where(e => e.MaDotThiGUID == obj.MaDotThiGUID).FirstOrDefault();
                if (dt == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<DotThi>(nhatKy, dt, obj.MaDotThiGUID, LOAIDOITUONG.ĐỢT_THI, LOAITHAOTAC.Chỉnh_sửa);
                dt.MaHocKyGUID = obj.MaHocKyGUID;
                dt.TenDotThi = obj.TenDotThi;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<DotThi>(nhatKy, dt);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(string guidIdDotThi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var dt = db.DotThiDbSet.Where(e => e.MaDotThiGUID == guidIdDotThi).FirstOrDefault();
                if (dt == null)
                    return CODE_RESULT_RETURN.ThatBai;
                if (dt.DotThi_LopHocPhan != null && dt.DotThi_LopHocPhan.Count > 0)
                    return CODE_RESULT_RETURN.ThatBai;
                else
                {
                    db.DotThiDbSet.Remove(dt);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<DotThi>(nhatKy, dt, dt.MaDotThiGUID, LOAIDOITUONG.ĐỢT_THI, LOAITHAOTAC.Xóa);
                    BizNhatKyHoatDong.Add<DotThi>(nhatKy, null);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
            }
        }
    }

    public class LichThi
    {
        public string NgayThi { get; set; }
        public string GioThi { get; set; }
        public string MaHocPhan { get; set; }
        public string TenLopHocPhan { get; set; }
        public string SoTC { get; set; }
        public string SoHocVien { get; set; }
        public string ThoiGianThi { get; set; }
        public string HinhThucThi { get; set; }
        public string PhongThi { get; set; }

        public static List<LichThi> GetList(DotThi dotThi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var dotThiLopHocPhan = BizDotThi_LopHocPhan.GetList(dotThi.MaDotThiGUID);
                if (dotThiLopHocPhan != null && dotThiLopHocPhan.Count > 0)
                {
                    List<LichThi> ls = new List<LichThi>();
                    foreach (var item in dotThiLopHocPhan)
                    {
                        LichThi lt = new LichThi();
                        lt.GioThi = item.GioThi;
                        lt.HinhThucThi = item.HinhThucThi;
                        var lhp = BizLopHocPhan.GetByID(item.MaLopHocPhanGUID);
                        if (lhp != null)
                        {
                            var hp = BizHocPhan.GetByID(lhp.MaHocPhanGUID);
                            if (hp != null)
                            {
                                lt.MaHocPhan = hp.MaHocPhan;
                                lt.SoTC = hp.SoTinChi.ToString();
                            }
                            lt.TenLopHocPhan = lhp.TenLopHocPhan;
                        }
                        lt.NgayThi = item.NgayThi.HasValue ? string.Format("{0:dd/MM/yyyy}", item.NgayThi.Value) : "";
                        var ph = BizPhongHoc.GetByID(item.MaPhongHoc);
                        if (ph != null)
                            lt.PhongThi = ph.TenPhongHoc;
                        var lsHV = BizLopHocPhan_BangDiem_HocVien.GetListHocVien(item.MaDotThiGUID, item.MaLopHocPhanGUID);
                        if (lsHV != null && lsHV.Count > 0)
                        {
                            lt.SoHocVien = lsHV.Count.ToString();
                        }
                        else
                            lt.SoHocVien = "0";
                        lt.ThoiGianThi = "120";
                        ls.Add(lt);
                    }
                    return ls;
                }
                else
                    return null;
            }
        }
    }
}
